package ro.upt.dcda.pricezone.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class Config {

	private static List<ServerInfo> servers;

	public static class ServerInfo {
		public ServerInfo(String name, double xmin, double xmax, double ymin,
				double ymax, String queue, String queueResp) {
			this.name = name;
			this.xmin = xmin;
			this.xmax = xmax;
			this.ymin = ymin;
			this.ymax = ymax;
			this.queue = queue;
			this.queueResp = queueResp;
		}

		private String name;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public double getXmin() {
			return xmin;
		}

		public void setXmin(double xmin) {
			this.xmin = xmin;
		}

		public double getXmax() {
			return xmax;
		}

		public void setXmax(double xmax) {
			this.xmax = xmax;
		}

		public double getYmin() {
			return ymin;
		}

		public void setYmin(double ymin) {
			this.ymin = ymin;
		}

		public double getYmax() {
			return ymax;
		}

		public void setYmax(double ymax) {
			this.ymax = ymax;
		}

		public String getQueue() {
			return queue;
		}

		public void setQueue(String queue) {
			this.queue = queue;
		}

		public String getQueueResp() {
			return queueResp;
		}

		public void setQueueResp(String queueResp) {
			this.queueResp = queueResp;
		}

		private double xmin;
		private double xmax;
		private double ymin;
		private double ymax;
		private String queue;
		private String queueResp;
	}

	public static List<ServerInfo> getServerList() {
		load();
		return new ArrayList<ServerInfo>(servers);
	}

	private static synchronized void load() {
		if (servers != null) {
			return;
		}
		Properties prop = readProperties();
		int serverCount = Integer.valueOf(prop.getProperty("nrservers"));
		servers = new ArrayList<ServerInfo>(serverCount);
		System.out.println("reading " + serverCount + " servers");
		for (int i = 1; i <= serverCount; i++) {
			String[] serverProp = prop.getProperty("server" + i).split(" ");
			String name = serverProp[0];
			double xmin = Double.valueOf(serverProp[1]);
			double xmax = Double.valueOf(serverProp[2]);
			double ymin = Double.valueOf(serverProp[3]);
			double ymax = Double.valueOf(serverProp[4]);
			servers.add(new ServerInfo(name, xmin, xmax, ymin, ymax,
					serverProp[5], serverProp[6]));
		}
	}

	private static Properties readProperties() {
		Properties prop = new Properties();
		try {
			prop.load(Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("server.properties"));
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
		}
		return prop;
	}
}
